package com.genius.workflow.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.genius.adminmanager.other.entity.ResponseJson;
import com.genius.workflow.config.DefaultConfiguration;

/**
 * @Author: woy
 * @Description: 公共上传图片控制器
 * @Date: Created in 2020/2/3 19:28
 * @param 
 */
@Controller
@RequestMapping("/upload")
public class UploadContorller {
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	@Autowired
	private OSSClientContorller ossUtil;
	
	@ResponseBody
    @PostMapping("/imagesOss")
    public JSONObject uploadOssImg(@RequestParam("file") MultipartFile file){

		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		response.putAll(ossUtil.uploadImg2Oss(file));
        return response;

    }
	@ResponseBody
    @PostMapping("/filesOss")
    public JSONObject uploadOssFiles(@RequestParam("file") MultipartFile file){

		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		response.putAll(ossUtil.uploadFileOss(file));
        return response;

    }
	@ResponseBody
    @PostMapping("/videoOss")
    public JSONObject uploadOssVideo(@RequestParam("file") MultipartFile file){
		System.out.print("osss-----------");
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		response.putAll(ossUtil.uploadVideoOss(file));
        return response;

    }
	
	
	@ResponseBody
    @PostMapping("/images")
    public JSONObject uploadImg(@RequestParam("file") MultipartFile file){
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
//		String url = "D:\\takeawaylog\\upload\\imgs\\";
		String url = "";
		try {
			File path = new File(ResourceUtils.getURL("classpath:").getPath());
			File upload = new File(path.getAbsolutePath(),"genius/uploads/images/");
			if(!upload.exists()) upload.mkdirs();
			url = upload.getAbsolutePath();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String src = "";
        String prefix="";
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                String originalName = file.getOriginalFilename();
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
                Date date = new Date();
                String uuid = UUID.randomUUID()+"";
                src = sdf.format(date)+"/"+uuid+"." + prefix;
                String filepath = url+"/"+src;
                File files=new File(filepath);
                //打印查看上传路径
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                response.put(ResponseJson.code,0);
                response.put(ResponseJson.src,src);
                return response;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            	response.put(ResponseJson.status, 1000);
    			response.put(ResponseJson.msg, "上传图片失败");
            }
        }
        return response;

    }
	@ResponseBody
    @PostMapping("/imagese")
    public JSONObject uploadImge(MultipartFile file){
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
//		String url = "D:\\takeawaylog\\upload\\imgs\\";
        String url = "D:\\img\\notice_info\\";
//		try {
//			File path = new File(ResourceUtils.getURL("classpath:").getPath());
//			File upload = new File(path.getAbsolutePath(),"genius/uploads/images/");
//			if(!upload.exists()) upload.mkdirs();
//			url = upload.getAbsolutePath();
//		} catch (FileNotFoundException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		String src = "";
        String prefix="";
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                String originalName = file.getOriginalFilename();
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
                Date date = new Date();
                String uuid = UUID.randomUUID()+"";
                src = sdf.format(date)+"/"+uuid+"." + prefix;
                String filepath = url+"/"+src;
                File files=new File(filepath);
                //打印查看上传路径
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                response.put(ResponseJson.code,0);
                response.put(ResponseJson.src,src);
                return response;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            	response.put(ResponseJson.status, 1000);
    			response.put(ResponseJson.msg, "上传图片失败");
            }
        }
        return response;

    }
	
	@ResponseBody
    @PostMapping("/files")
    public JSONObject uploadFile(@RequestParam("file") MultipartFile file){
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
//		String url = "D:\\takeawaylog\\upload\\imgs\\";
		String url = "";
		try {
			File path = new File(ResourceUtils.getURL("classpath:").getPath());
			File upload = new File(path.getAbsolutePath(),"genius/uploads/files/");
			if(!upload.exists()) upload.mkdirs();
			url = upload.getAbsolutePath()+DefaultConfiguration.file_separator;
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String src = "";
        String prefix="";
        //保存上传
        OutputStream out = null;
        InputStream fileInput=null;
        try{
            if(file!=null){
                String originalName = file.getOriginalFilename();
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);
                Date date = new Date();
                String uuid = UUID.randomUUID()+"";
                src = sdf.format(date)+"/"+uuid+"." + prefix;
                String filepath = url+"/"+src;
                

                File files=new File(filepath);
                //打印查看上传路径
                if(!files.getParentFile().exists()){
                    files.getParentFile().mkdirs();
                }
                file.transferTo(files);
                response.put(ResponseJson.code,0);
                response.put(ResponseJson.src,src);
                return response;
            }

        }catch (Exception e){
        }finally{
            try {
                if(out!=null){
                    out.close();
                }
                if(fileInput!=null){
                    fileInput.close();
                }
            } catch (IOException e) {
            	response.put(ResponseJson.status, 1000);
    			response.put(ResponseJson.msg, "上传文件失败");
            }
        }
        return response;

    }
	@ResponseBody
    @PostMapping("/ImgOssDelete")
    public JSONObject uploadImgOssDelete(@RequestBody JSONObject json){
		String key = json.getString("key");
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		try {
			ossUtil.deleteImgOss(key);
			response.put(ResponseJson.code,0);
            response.put(ResponseJson.msg,"删除成功");
		} catch (Exception e) {
			response.put(ResponseJson.status, 1000);
			response.put(ResponseJson.msg, "删除失败");
			e.printStackTrace();
		} 
		
		return response;
    }
	@ResponseBody
    @PostMapping("/fileOssDown")
    public JSONObject fileOssDown(@RequestBody JSONObject json){

		String key = json.getString("key");
		//String localFile = json.getString("localFile");
		JSONObject response = new JSONObject();
		response.put(ResponseJson.code, 0);
		try {
			String downUrl = ossUtil.getDownUrl(key);
			response.put(ResponseJson.code,0);
			response.put("downUrl",downUrl);
            response.put(ResponseJson.msg,"获取Oss文件下载地址成功");
		} catch (Exception e) {
			response.put(ResponseJson.status, 1000);
			response.put(ResponseJson.msg, "获取Oss文件下载地址失败");
			e.printStackTrace();
		} 
		
		return response;
    }
}
